<?php

class Base_LayoutObject extends Erra_Fields_Base_Object {

    /**
     * Constructor
     */
    function __construct($objectId, $buildArgs = null) {

        parent::__construct($objectId, $buildArgs);
        $feedData = $this->getDataArray();
        $this->change('description=' . __('Page configuration'));
        if (Erra_System_AccessControl::is("dev")) {
            $url = Erra_Fields_Text::create('page_url')
                    ->prepend('<b>' . __('Basics') . '</b><br/>')
                    ->setDescription(__('URL/Route machine name (set `default` for the homepage)'))
                    ->setSize(70)
                    ->setInAdminList(true);
        } else {
            $url = Erra_Fields_TextDisabled::create('page_url')
                    ->prepend('<b>' . __('Basics') . '</b><br/>')
                    ->change('description=' . __('URL/Route machine name'))
                    ->setSize(70)
                    ->setInAdminList(true);
        }
        $searchByUrl = $url->createTextSearch()
                ->setDescription('Rechercher par URL')
                ->setOperator('LIKE ?%');

        $className = Erra_Fields_ComboBox::create('layoutFile')
                ->setDescription(__('Layout file'))
                ->setValue('DefaultLayout.phtml')
                ->fill($feedData['layouts']);

        $viewRegion = Erra_Fields_Base_Field::create('viewRegion');
        $regionName = Erra_Fields_ComboBox::create('region_name')
                ->setDescription(__('Select a region'))
                ->fill(array('' => 'Choose a region', 'header' => 'Header', 'highlighted' => 'Highlighted', 'help' => 'Help', 'content' => 'Content', 'sidebar_first' => 'First sidebard', 'sidebar_second' => 'Second sidebar', 'footer' => 'Footer'));

        $viewName = Erra_Fields_ComboBox::create('view_name')
                ->fill($feedData['views'])
                ->setDescription(__('Default view assigned to this region'))
                ->setInAdminList(true);

        $viewParam = Erra_Fields_Text::create('view_param')
                ->setDescription(__('URL parameter(s) assigned to this region'));
        $viewRegion->addFields($regionName, $viewName, $viewParam)->multiply(7)->setDescription('Region');


        $menu_name = Erra_Fields_Text::create('menu_name')
                ->setDescription(__('Menu name for this page'))
                ->setInAdminList(true)
                ->_localize();

        $in_nav = Erra_Fields_MultipleCheckBox::create('exclude_from_navigation')
                ->setDescription(__('Exclude from navigation ?'))
                ->fill(array('yes'=>'Out of navigation'))
                ->setInAdminList(true);

        $ac_grant = Erra_Fields_Text::create('ac_grant')
                ->prepend('<br/><b>' . __('Access control') . '</b><br/>')
                ->setDescription(__('Authorized roles'))
                ->setSize(70)
                ->setInAdminList(true);

        $ac_on_deny = Erra_Fields_Text::create('ac_on_deny')
                ->setDescription(__('URL to redirect on deny (application.http_webroot for homepage)'))
                ->setSize(70);

        $aliases = Erra_Fields_Text::create('page_aliases')
                ->prepend('<br/><b>' . __('SEO informations') . '</b><br/>')
                ->setDescription(__('SEO friendly alias for this page'))
                ->_localize();

        $seo_title = Erra_Fields_Text::create('page_seo_title')
                ->setDescription(__('Page title'))
                ->setInAdminList(true)
                ->setSize(70)
                ->_localize();

        $seo_desc = Erra_Fields_TextArea::create('page_seo_desc')
                ->setDescription(__('Meta description for this page'))
                ->setCols(70)
                ->setRows(2)
                ->_localize();

        $seo_keywords = Erra_Fields_TextArea::create('page_seo_keywords')
                ->setDescription(__('Page keywords'))
                ->setCols(70)
                ->setRows(2)
                ->_localize();


        $this->setDescription(__('Application pages management'));
        $this->addFields($searchByUrl, $url, $className, $viewRegion, $in_nav, $menu_name, $ac_grant, $ac_on_deny, $aliases, $seo_title, $seo_desc, $seo_keywords);
    }

    // TODO: get regions for each layout
    public function getDataArray() {
        // get layout list
        $files = Erra_Core_File::spl_scanRecursiveDir(DOCUMENT_ROOT . '/application/Layouts');
        $layoutList = array();
        foreach ($files as $layoutFile) {
            if (strstr($layoutFile, '.phtml') === false || strstr($layoutFile, 'Admin_'))
                continue;

            $layoutData = file_get_contents($layoutFile);
            $layoutFile = basename($layoutFile);
            $layoutList[$layoutFile] = $layoutFile;
            // Get regions
            if (preg_match_all('|<region(.*?)</region>|ms', $layoutData, $matches)) {
                foreach ($matches[1] as $mt) {
                    $region_name = str_replace(array('>', '"', "'", ' ', ''), '', $mt);
                    $region_name = str_replace(array('name='), '', $region_name);
                    if (ctype_alnum(str_replace('_', '', $region_name))) {
                        
                    }
                }
            }
        }

        // Get view list
        $files = Erra_Core_File::spl_scanRecursiveDir(DOCUMENT_ROOT . '/application/Views');
        $viewList = array();
        foreach ($files as $viewFile) {
            if (strstr($viewFile, '.phtml') === false || strstr($viewFile, 'Admin_'))
                continue;
            $viewFile = basename($viewFile);
            $viewList[$viewFile] = $viewFile;
        }

        $dataArray = array();
        $dataArray['layouts'] = $layoutList;
        $dataArray['views'] = $viewList;

        return $dataArray;
    }

    public function getTableName() {
        return 'ufm__pages';
    }

}

?>